package net.lwf.handler.wp;

import java.util.ArrayList;
import java.util.List;

import lwf.handler.IProcessHandler;
import lwf.handler.ProcessHandler;
import lwf.handler.helper.DisplayFormatter;
import net.lwf.entity.QuestionInfo;
import net.lwf.exception.AppException;
import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.handler.wp.helper.IWordProblem;
import net.lwf.handler.wp.service.WordProblemService;
import net.lwf.hibernate.entity.wp.WpData;
import net.lwf.pdf.util.FontUtil;
import net.lwf.pdf.util.PdfPCellUtil;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

public class WordProblemHandler  extends ProcessHandler implements IProcessHandler {
	
	private String type;
	
	public WordProblemHandler(String grade, String type) {
		super(grade);
		this.type = type;
	}

	public DisplayFormatter getDisplayFormatter() {
		return null;
	}

	public String getQuestionDesc() {
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		WpData data = new WordProblemService().getSingleData(getGrade(), this.type);
		return this.process(data);
	}

	@Override
	public List<QuestionInfo> generateMultiple() {
		List<WpData> data = new WordProblemService().getMultipleData(getGrade(), this.type);
		return this.process(data);
	}

	@Override
	public void getContent(Document document) throws DocumentException {
		
	}

	@Override
	public int getRows() {
		return IHandlerConstant.WORD_PROBLEMS_FOR_PDF_GENERATION;
	}

	@Override
	public int getColumns() {
		return 1;
	}

	@Override
	public boolean isPortrait() {
		return true;
	}

	@Override
	public String getPdfInstructionalText() {
		return null;
	}

	@Override
	public String getPdfNote() {
		return null;
	}
	
	public Element processCellData(QuestionInfo question) {
		PdfPTable table = new PdfPTable(1);

        table.setSpacingBefore(20);

        //Row 1
        PdfPCell cell;
        cell = new PdfPCell(new Phrase("# " + question.getQuestionNumber(), FontUtil.getQuestionNumberFont()));
        cell.setBorder(0);
        table.addCell(cell);
        
        //row 2
         
        cell = PdfPCellUtil.getCell(question.getQuestion(), 0, 0);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.addCell(cell);
        
        table.completeRow();

		return table;
	}	
	
	private QuestionInfo process(WpData wpdata) {
		if (wpdata != null) {
			//wpdata.print();
			try {
				IWordProblem wp = (IWordProblem) Class.forName(
						wpdata.getClassName()).newInstance();
				return wp.generate(wpdata);
			} catch (InstantiationException iex) {
				throw new AppException(iex);
			} 
			catch (IllegalAccessException iaex) {
				throw new AppException(iaex);
			}
			catch (ClassNotFoundException cnfex) {
				throw new AppException(cnfex);
			}
		}
		return null;
	}
	
	private List<QuestionInfo> process(List<WpData> data) {
		List<QuestionInfo> qinfo = new ArrayList<QuestionInfo>();
		int nCounter = 1;
		for(WpData wpdata: data) {
			QuestionInfo question = this.process(wpdata);
			question.setQuestionNumber(nCounter);
			qinfo.add(question);
			nCounter++;
		}
		return qinfo;
	}

}
